package com.example.demo;

import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;

/**
 * 广播消息ctrl
 *  
 * @version 1.5
 * @author fuhw
 */
@Controller
public class PublishMsgController {
	
	private static int starCount = 1;
	
	@Autowired
	private MyWebSocketService socketService;

    @MessageMapping("/listener-event")
    @SendTo("/topic/getResponse")//订阅地址
    public ReponseMsg say(RequestMsg message) {
    	System.err.println("cur-star-count:" + starCount);
        return new ReponseMsg(  starCount++ + "您广播了一条消息:======" + message.getName());
    }
    
    @Scheduled(fixedRate=5000)
    public void sendMsg () {
    	try {
    		starCount++;
			socketService.sendMessage("hello" + starCount);
		} catch (IOException e) {
			e.printStackTrace();
		}
    }
}
